package it.application.smoc;

import it.application.smoc.List.CustomizedListView;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends CustomWindow {

	private boolean loggato= false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(loggato){mostraOpLoggato();}
        else{nascondiOpLoggato();}
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean("loggato", loggato);
        super.onSaveInstanceState(outState);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        loggato= savedInstanceState.getBoolean("loggato");
    }
    
    public void Login (View v){
    	String user= ((EditText) findViewById(R.id.txtUsername)).getText().toString();
    	String pass= ((EditText) findViewById(R.id.txtPassword)).getText().toString();
    	// TODO: Sostituire con login reale
    	if (user.equals("admin") && pass.equals("pass"))
    	{
    			loggato= true;
    			mostraOpLoggato();
    			v.getParent().requestLayout();
    	}
    	else Toast.makeText(v.getContext(), "Nome utente o password errati", Toast.LENGTH_SHORT).show();
    }
    
    public void GoToOrderInTheWorld(View v){
    	Intent intent=new Intent(MainActivity.this,OrderInTheWorldActivity.class);
    	startActivity(intent);
    }
    public void GoToNaplesDelegation(View v){
    	Intent intent=new Intent(MainActivity.this,NaplesDelegation.class);
    	intent.putExtra("it.application.smoc.Logged", loggato);
    	startActivity(intent);
    }
    public void GoToRegisteredKnights(View v){
    	Intent intent=new Intent(MainActivity.this,CustomizedListView.class);
    	startActivity(intent);
    }
    public void GoToRealCommission(View v){
    	Intent intent=new Intent(MainActivity.this,RCIActivity.class);
    	startActivity(intent);
    }
    
    private void mostraOpLoggato() {
    	((Button) findViewById(R.id.btnRegisteredKnights)).setVisibility(Button.VISIBLE);
    	((TextView) findViewById(R.id.lblUsername)).setVisibility(TextView.GONE);
    	((TextView) findViewById(R.id.lblPassword)).setVisibility(TextView.GONE);
    	((EditText) findViewById(R.id.txtUsername)).setVisibility(EditText.GONE);
    	((EditText) findViewById(R.id.txtPassword)).setVisibility(EditText.GONE);
    	((Button) findViewById(R.id.btnEnter)).setVisibility(Button.GONE);
	}
    
    private void nascondiOpLoggato() {
    	((Button) findViewById(R.id.btnRegisteredKnights)).setVisibility(Button.GONE);
    	((TextView) findViewById(R.id.lblUsername)).setVisibility(TextView.VISIBLE);
    	((TextView) findViewById(R.id.lblPassword)).setVisibility(TextView.VISIBLE);
    	((EditText) findViewById(R.id.txtUsername)).setVisibility(EditText.VISIBLE);
    	((EditText) findViewById(R.id.txtPassword)).setVisibility(EditText.VISIBLE);
    	((Button) findViewById(R.id.btnEnter)).setVisibility(Button.VISIBLE);
    }
}
